package cn.birdbaby.plugins.annotator.classannotator.gen.genannotator;

import cn.birdbaby.plugins.annotator.AbstractAnnotator;
import cn.birdbaby.plugins.annotator.Context;
import cn.birdbaby.plugins.common.CommonUtils;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiElement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author llorando 6/8/24 5:14 pm
 */
public abstract class AutoGenAnnotator extends AbstractAnnotator {


    public AutoGenAnnotator() {

    }


    public Map<String, PsiAnnotationMemberValue> buildAnnotationContent(Context context) {
        Map<String, PsiAnnotationMemberValue> valueMap = new HashMap<>();
        // Set the pkgName attribute
        PsiAnnotationMemberValue pkgNameValue = JavaPsiFacade.getElementFactory(context.getProject()).createExpressionFromText("\"" + getPkgName(context) + "\"", context.getCurrentElement());
        valueMap.put("pkgName", pkgNameValue);

        //只有包含sourcePath属性的Gen注解会跑进来
        if (hasSourcePath()) {
            //找了一天的api，谁想到直接引用就行
            PsiAnnotationMemberValue sourcePathValue = JavaPsiFacade.getElementFactory(context.getProject()).createExpressionFromText("Constants.GEN_API_SOURCE", context.getCurrentElement());
            valueMap.put("sourcePath", sourcePathValue);
        }

        if (hasMapperPath()) {
            PsiAnnotationMemberValue mapperPath = JavaPsiFacade.getElementFactory(context.getProject()).createExpressionFromText("Constants.CUSTOMER_MAPPER_PKG", context.getCurrentElement());
            valueMap.put("customerMapperPkgName", mapperPath);
        }
        return valueMap;
    }

    public abstract String getPkgName(Context context);

    public String getCustomerMapperPkgName() {
        return "";
    }

    public boolean hasSourcePath() {
        return false;
    }


    public boolean hasMapperPath() {
        return false;
    }

    @Override
    public boolean support(PsiElement psiElement) {
        return CommonUtils.shouldAddGen(psiElement.getContainingFile());
    }
}
